package vip.specimen.system.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.specimen.system.common.enums.SaveMethodEnum;
import vip.specimen.system.datasource.entity.SystemSaveConfigEntity;
import vip.specimen.system.datasource.validation.SystemSaveConfigTencentValidation;
import vip.specimen.system.repository.SystemSaveConfigRepository;
import vip.specimen.system.datasource.validation.SystemSaveConfigAliOssValidation;
import vip.specimen.system.datasource.validation.SystemSaveConfigQiNiuValidation;

import javax.annotation.Resource;
import java.util.Calendar;

/**
 * *@Author: 余浪人
 * *@Date：2021/3/8 14:39
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Service
public class SystemSaveConfigService {
    @Resource
    private SystemSaveConfigRepository systemSaveConfigRepository;

    /**
     * 配置初始化
     *
     * @return 配置
     */
    @Transactional
    SystemSaveConfigEntity initialize() {
        return this.systemSaveConfigRepository.save(new SystemSaveConfigEntity(SaveMethodEnum.QiNiu, "", "", "", ""));
    }

    /**
     * 获取配置信息
     *
     * @return 配置
     */
    public SystemSaveConfigEntity getConfig() {
        return this.systemSaveConfigRepository.count() == 0 ? this.initialize() : this.systemSaveConfigRepository.findFirstByOrderByCreateTimeDesc();
    }

    /**
     * 更新七牛云存储
     *
     * @param qiNiu 七牛对象
     * @return 配置
     */
    @Transactional
    public SystemSaveConfigEntity update(SystemSaveConfigQiNiuValidation qiNiu) {
        SystemSaveConfigEntity save = this.getConfig();
        save.setQiNiuSaveName(qiNiu.getQiNiuSaveName());
        save.setSaveMethod(SaveMethodEnum.QiNiu);
        save.setQiNiuAccessKey(qiNiu.getQiNiuAccessKey());
        save.setQiNiuSecretKey(qiNiu.getQiNiuSecretKey());
        save.setQiNiuDomain(qiNiu.getQiNiuDomain());
        save.setUpdateTime(Calendar.getInstance().getTime());
        this.systemSaveConfigRepository.save(save);
        return save;
    }

    /**
     * 更新阿里OSS存储
     *
     * @param ossValidation oss验证
     * @return 配置
     */
    @Transactional
    public SystemSaveConfigEntity update(SystemSaveConfigAliOssValidation ossValidation) {
        SystemSaveConfigEntity save = this.getConfig();
        save.setSaveMethod(SaveMethodEnum.ALiYun);
        save.setAliSupportCname(ossValidation.isAliSupportCname());
        save.setAliAccessKey(ossValidation.getAliAccessKey());
        save.setAliBucketName(ossValidation.getAliBucketName());
        save.setAliAccessKeySecret(ossValidation.getAliAccessKeySecret());
        save.setAliEndpoint(ossValidation.getAliEndpoint());
        save.setUpdateTime(Calendar.getInstance().getTime());
        this.systemSaveConfigRepository.save(save);
        return save;
    }

    /**
     * 更新腾讯云对象存储
     *
     * @param tencentValidation 腾讯云验证
     * @return 配置
     */
    @Transactional
    public SystemSaveConfigEntity update(SystemSaveConfigTencentValidation tencentValidation) {
        SystemSaveConfigEntity save = this.getConfig();
        save.setSaveMethod(SaveMethodEnum.Tencent);
        save.setTencentBucketName(tencentValidation.getTencentBucketName());
        save.setTencentDomain(tencentValidation.getTencentDomain());
        save.setTencentRegion(tencentValidation.getTencentRegion());
        save.setTencentSecretId(tencentValidation.getTencentSecretId());
        save.setTencentSecretKey(tencentValidation.getTencentSecretKey());
        save.setUpdateTime(Calendar.getInstance().getTime());
        this.systemSaveConfigRepository.save(save);
        return save;
    }
}
